package com.wdress.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.DynaActionForm;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import com.wdress.base.BaseAction;
import com.wdress.model.Wdress;
import com.wdress.model.Wkeyword;

public class WdressAction extends BaseAction{

	@Override
	public String executeAction(DynaActionForm form, Session session,
			HttpServletRequest arg2, HttpServletResponse arg3,String lang) throws Exception {
		String id=form.getString("id");
		Query query=session.createQuery("from Wdress where dress_id=? and lang=?");
		query.setString(0,id);
		query.setString(1,lang);
		Wdress wdress=(Wdress) query.uniqueResult();
		arg2.setAttribute("wdress", wdress);
		query = session.createQuery("from Wkeyword where types='categorie' and keyword=? and lang=?");
		query.setString(0, wdress.getDress_type());
		query.setString(1, lang);
		Wkeyword categorieObj= (Wkeyword) query.uniqueResult();
		arg2.setAttribute("categorieObj", categorieObj);
		query=session.createQuery("from Wdress where dress_id<>? and dress_type=? and lang=? order by abs(dress_id-?)");
		query.setString(0,id);
		query.setString(1,categorieObj.getKeyword());
		query.setString(2,lang);
		query.setString(3,id);
		List<Wdress> wdressList=query.setMaxResults(10).list();
		arg2.setAttribute("wdressList", wdressList);
		initLangUrl(session, arg2, wdress.getDress_id());
		return "success";
	}
	
	public void initLangUrl(Session session, HttpServletRequest arg2,String id){
		Query query = session.createQuery("from Wdress where dress_id=?");
		query.setString(0, id);
		List<Wdress> urlObj= query.list();
		Wdress temp=null;
		for(int i=0;i<urlObj.size();i++){
			temp=urlObj.get(i);
			if("en".equals(temp.getLang())){
				arg2.setAttribute("enUrl", "http://www.superbdress.com/"+temp.getDress_url()+"_"+temp.getDress_id()+".html");
			}else{
				arg2.setAttribute(temp.getLang()+"Url", "http://"+temp.getLang()+".superbdress.com/"+temp.getDress_url()+"_"+temp.getDress_id()+".html");
			}
			
		}
	}

}
